﻿<!DOCTYPE html>
<html>
<head>
	<meta charset="UTF-8">
	<link rel="stylesheet" type="text/css" href="../themes/default/easyui.css">
	<link rel="stylesheet" type="text/css" href="../themes/icon.css">
	<link rel="stylesheet" type="text/css" href="../demo.css">
	<script type="text/javascript" src="../jquery.min.js"></script>
	<script type="text/javascript" src="../jquery.easyui.min.js"></script>
	<script type="text/javascript" src="../locale/easyui-lang-zh_CN.js"></script>
    <script type="text/javascript" src="../extend.js"></script>
	<script type="text/javascript" src="../jquery.cookie.js"></script>
    <script>
		var searchArg = {};
        var dataListCache = null;
        var dataCache = null;
        var appId = GetQueryString("moduleid");
        window.onresize=calSize;
        $(document).ready(function(){
            setData();
            calSize();
		});
		function pageChanging(pageNumber, pageSize) {
            showMask("查找中");
            $.get('../../api/bank/list', { Args: searchArg, Length: pageSize, Pos: (pageNumber-1) * pageSize }, function (data) {
                if (data.Result == 0) {
                    dataListCache = JSON.parse(data.Data);
                    $("#bankList").datagrid("loadData", dataListCache);
                    $("#bankList").datagrid('getPager').pagination({ total: data.Count, pageNumber: pageNumber, onSelectPage: pageChanging });
                } else {
                    $.messager.alert('提示', data.Message, 'error');
                }
                hideMask();
            }, 'json');
        }
        function setData() {
            pageChanging(1,10);
        }
        window.onresize=calSize;
        function calSize(){
            var height=window.parent.document.getElementById("mainFrame").clientHeight;
            var width=window.parent.document.getElementById("mainFrame").clientWidth;
            window.parent.document.getElementById(appId).height = height-32;
            $("#bankList").datagrid('resize',{height:height-76,width:width-15});
            $("#toolbar").panel({width:width-15});
            $("#editToolbar").panel({width:width-15});
            $("#editZone").panel('resize',{height:height-76,width:width-15});
        }
        
        function initVal(){
            $('#AutoId').val(dataCache.AutoId);
            $('#Flag').val(dataCache.Flag);
            $('#Site').val(dataCache.Site);
			$('#State').val(dataCache.State);
            $('#Code').textbox("setValue",dataCache.Code);
            $('#Name').textbox("setValue",dataCache.Name);
			$('#BankDesc').textbox("setValue",dataCache.BankDesc);
        }
        function createNewRecord(){
            dataCache = new Object();
            initVal();
            $('#editContent').window('open');
        }
		function deleteRecord(){
			if (dataCache == null||dataCache.AutoId==0) {
                $.messager.alert("错误", "请先选中要删除的对象", "error");
                return;
            }
			$.messager.confirm('删除提示', '确定删除该记录?', function(r){
				if (r){
					showMask();
					$.post("../../api/bank/remove",dataCache,function(response){
						if(response.Result==0){
							for(var i=0;i<dataListCache.length;i++){
								if(dataListCache[i].AutoId == dataCache.AutoId){
									dataListCache.splice(i,1);
									break;
								}
							}					
							dataCache = null;
							$('#bankList').datagrid('loadData', dataListCache); 
							$.messager.alert("信息","操作成功","info");
						}else{
							$.messager.alert("错误",response.Message,"error");
						}
					})
					.error(function(){
						$.messager.alert("错误","系统内部错误","error");
					})
					.complete(function() { 
						hideMask();
					});	
				}
			});		
		}
        function editRecord(rowIndex,rowData){
            dataCache = rowData;
            initVal();
            $('#editContent').window('open');
        }
		function selectRecord(rowIndex,rowData){
			dataCache = rowData;
		}
        function submitChange(){
            if (dataCache == null) {
                $.messager.alert("错误", "无法获取被编辑的对象", "error");
                return;
            }
            showMask();
			var oldData = JSON.parse(JSON.stringify(dataCache));
            dataCache.Flag = $("#Flag").val();
			dataCache.State = $("#State").val();
            dataCache.Code=$("#Code").textbox("getValue");
            dataCache.Name=$("#Name").textbox("getValue");
			dataCache.BankDesc=$("#BankDesc").textbox("getValue");
            $.post("../../api/bank/set",dataCache,function(response){
                if(response.Result==0){
                    dataCache = JSON.parse(response.Data);
                    if(dataCache.Flag != 1){
                        for(var i=0;i<dataListCache.length;i++){
                            if(dataListCache[i].AutoId == dataCache.AutoId){
                                dataListCache.splice(i,1,dataCache);
								//dataListCache[i] = dataCache;
								console.log('列表第'+i+'项被更新');
                                break;
                            }
                        }
                    }else{
						dataListCache.splice(0,0,dataCache);
						console.log('新项被追加到列表');
					}
                    $('#bankList').datagrid('loadData', dataListCache); 
                    $.messager.alert("信息","操作成功","info");
                    $('#editContent').window('close');
                }else{
                    $.messager.alert("错误",response.Message,"error");
					dataCache = oldData;
                }
            })
            .error(function(){
                $.messager.alert("错误","系统内部错误","error");
				dataCache = oldData;
            })
            .complete(function() { 
                hideMask();
            });
        }
        function boolFormatter(value,row,index){
            if(value){
                return "是";
            }else{
                return "否";
            }
        }
    </script>
<head>
<body onload = "hideMask()">
    <script>showMask('正在载入数据');</script>
	<div id="toolbar" class="easyui-panel" style="padding:1px;">
		<a href="#" class="easyui-linkbutton" data-options="onClick:createNewRecord,iconCls:'icon-edit',plain:true">新建</a>
		<a href="#" class="easyui-linkbutton" data-options="onClick:deleteRecord,iconCls:'icon-remove',plain:true">删除</a>
        <a href="#" class="easyui-linkbutton" data-options="onClick:setData,iconCls:'icon-search',plain:true">搜索</a>
	</div>
    <div style="margin:2px"></div>
    <table id="bankList" class="easyui-datagrid"
        data-options="pagination:true,rownumbers: true,singleSelect:true,method:'get',onClickRow:selectRecord,onDblClickRow:editRecord,striped:true, ">
        <thead>
            <tr>
                <th data-options="field:'AutoId',hidden:true"></th>
                <th data-options="field:'Flag',hidden:true"></th>
                <th data-options="field:'State',hidden:true"></th>
                <th data-options="field:'Code',width:300,align:'right'">分行代号</th>
                <th data-options="field:'Name',width:200,align:'right'">分行名称</th>
				<th data-options="field:'BankDesc',width:200,align:'right'">所属银行</th>
            </tr>
        </thead>
    </table>
    <div id="editContent" class="easyui-window" title="编辑框" data-options="noheader:true,maximized:true,modal:true,closed:true,iconCls:'icon-save',border:false" style="padding:5px 5px 0px 5px">
        <div class="easyui-panel" id="editToolbar" style="padding:1px;width:100%">
            <a href="#" class="easyui-linkbutton" data-options="onClick:submitChange,iconCls:'icon-save',plain:true">保存</a>
            <a href="#" class="easyui-linkbutton" data-options="onClick:initVal,iconCls:'icon-no',plain:true">取消</a>
            <a href="#" class="easyui-linkbutton" data-options="onClick:function(){$('#editContent').window('close');},iconCls:'icon-blank',plain:true">关闭</a>
        </div>
        <div style="margin:2px"></div>
        <div id="editZone" class="easyui-panel" style="padding:4px 4px 0px 4px" >
            <table>
                <tr>
                    <td><input class="easyui-textbox" id="Code"  style="width:300px" data-options="label:'分行代号:',required:true"></td>
                    <td>&nbsp;</td>
                    <td><input class="easyui-textbox" id="Name"  style="width:300px" data-options="label:'分行名称:'"></td>
					<td>&nbsp;</td>
                    <td><input class="easyui-textbox" id="BankDesc"  style="width:300px" data-options="label:'所属银行:'"></td>
                </tr>
            </table>
            <div hidden>
                <p id="AutoId"></p>
                <p id="Flag"></p>
                <p id="Site"></p>
                <p id="State"></p>
            </div>
        </div>
	</div>
</body>
</html>